//
//  AlertItem.swift
//  eBooks
//
//  Created by JunMing on 2019/12/14.
//  Copyright © 2019 赵俊明. All rights reserved.
//

import UIKit

class AlertItem {
    public var action:((_ item:AlertItem)->Void)?
    var title:String?
    var icon:UIImage?
    var font:UIFont?
    var eventName:String?
    var textColor = UIColor.gray
    var priority = 100
    // 这个是谷歌广告使用
    var rootVc:UIViewController?
    
    init(title:String,icon:UIImage?) {
        self.title = title
        self.icon = icon
    }
}

protocol AlertProtocol {
    func remove()
    func torchAction(_ item:AlertItem)
}

class AlertManager {
    public var didselectAction:((_ item:AlertItem)->Void)?
    private var alertCon = AlertContainer()
    private var backView = UIView()
    init(contriner:UIView?,items:Array<AlertItem>) {
        if let view = contriner {
            backView.frame = view.bounds
            view.addSubview(backView)
        }else {
            let window = UIApplication.shared.keyWindow
            backView.frame = window!.bounds
            window?.addSubview(backView)
        }
        
        alertCon.delegate = self
        backView.backgroundColor = UIColor.RGBA(100, 100, 100, 0.5)
        backView.addSubview(alertCon)
        
        let item = AlertItem(title: "取消", icon: nil)
        item.priority = 0
        alertCon.items.append(item)
        
        for itemValue in items.reversed() {
            alertCon.items.insert(itemValue, at: 0)
        }
    }
    public func appendItem(_ item:AlertItem) {
        alertCon.items.insert(item, at: 0)
    }
    
    public func update() {
        let count = alertCon.items.count*(44+1)+5
        alertCon.snp.remakeConstraints { (make) in
            make.width.equalTo(backView)
            make.height.equalTo(count)
            if #available(iOS 11.0, *) {
                make.bottom.equalTo(backView.safeAreaLayoutGuide.snp.bottom)
            }else{
                make.bottom.equalTo(backView.snp_bottom)
            }
        }
        alertCon.update()
    }
}

extension AlertManager:AlertProtocol {
    func remove() {
        UIView.animate(withDuration: 0.3, animations: {
            self.backView.alpha = 0
        }) { _ in
            self.backView.removeFromSuperview()
        }
    }
    func torchAction(_ item:AlertItem) {
        item.action?(item)
        remove()
    }
}

class AlertContainer: UIView {
    var items = [AlertItem]()
    var delegate:AlertProtocol?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.RGB(240, 240, 240)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update() {
        var tempConstrainr = self.snp_top
        for (index,item) in items.enumerated() {
            let btn = UIButton(type: UIButton.ButtonType.system)
            btn.backgroundColor = UIColor.white
            btn.tintColor = item.textColor
            btn.tag = index + 100
            btn.addTarget(self, action: #selector(touchAction(_:)), for: UIControl.Event.touchUpInside)
            btn.setTitle(item.title, for: UIControl.State.normal)
            if let image = item.icon?.origin {
                btn.setImage(image, for: UIControl.State.normal)
                // btn.contentHorizontalAlignment = .center
                // btn.imageEdgeInsets = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 15)
                // btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
            }
            if let font = item.font { btn.titleLabel?.font = font }
            addSubview(btn)
            btn.snp.makeConstraints { (make) in
                make.width.equalTo(self)
                make.height.equalTo(44)
                if item.priority == 0 {
                    make.top.equalTo(tempConstrainr).offset(6)
                }else{
                    make.top.equalTo(tempConstrainr).offset(1)
                }
            }
            tempConstrainr = btn.snp_bottom
        }
    }
    
    @objc func touchAction(_ sender:UIButton) {
        let item = items[sender.tag - 100]
        if item.title == "取消" {
            delegate?.remove()
        }else {
            delegate?.torchAction(item)
        }
    }
}
